package com.thrc.borrow.controller;

import java.io.File;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.alibaba.fastjson.JSONObject;
import com.thrc.borrow.utils.PropertiesUtil;
import com.thrc.borrow.entity.Personnel;
import com.thrc.borrow.service.PersonnelService;
import com.thrc.borrow.utils.ResponseUtil;
import com.thrc.borrow.utils.ReturnCode;
import com.thrc.borrow.vo.PersonnelVo;

@Controller
@RequestMapping("/personnel")
public class PersonnelController {

	@Autowired
	private PersonnelService personnelService;

	@RequestMapping("/list/page")
	public String goUserJsp() {

		return "sys/personnelList";
	}

	@RequestMapping("/getById")
	public void selectById(HttpServletResponse response, Personnel t) {
		PersonnelVo vo = personnelService.selectById(t);
		ResponseUtil.print(response, vo);
	}

	@RequestMapping("/selectAll")
	public void selectAll(HttpServletResponse response, Personnel t) {
		try {
			List<PersonnelVo> UserList = personnelService.selectAll(t);
			ResponseUtil.print(response, UserList);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/search")
	public void search(HttpServletResponse response, Personnel t) {
		try {
			List<PersonnelVo> personnelList = personnelService.selectAll(t);
			if (personnelList.size() > 0) {
				ResponseUtil.print(response, ReturnCode.SUCCESS_CODE, "操作成功", personnelList);
			} else {
				ResponseUtil.print(response, ReturnCode.FAIL_CODE, "没有查询到数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/selectByPage")
	public void selectByPage(HttpServletResponse response, Personnel t) {

		JSONObject retJson = new JSONObject();
		try {
			int totleCount = personnelService.selectCount(t);
			retJson.put("total", totleCount);
			if (totleCount == 0) {
				retJson.put("data", null);
				ResponseUtil.print(response, retJson);
				return;
			}
			List<PersonnelVo> UserList = personnelService.selectByPage(t);
			retJson.put("data", UserList);
			ResponseUtil.print(response, retJson);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/insertPersonnel")
	public void insert(HttpServletResponse response, HttpServletRequest request, Personnel t) {

		try {
			t.setCreateTime(new Date());
			personnelService.insert(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/updatePersonnel")
	public void update(HttpServletResponse response, Personnel t) {
		try {
			personnelService.update(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/deletePersonnel")
	public void delete(HttpServletResponse response, Personnel t) {

		try {
			personnelService.delete(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/fileupload")
	public void filesUpload(HttpServletResponse response, HttpServletRequest request) throws Exception {
		// 取得上传的文件
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

		CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("uploadFileName");
		// 得到文件名称
		String realFileName = file.getOriginalFilename();
		String suffix = realFileName.substring(realFileName.indexOf("."), realFileName.length());

		String fileRealPath = PropertiesUtil.getKey(PropertiesUtil.KEY.FILE_UPLOAD_PATH);
		// 判断文件夹是否存在
		File targerFile = new File(fileRealPath);
		// 判断是否存在目录
		if (!targerFile.exists()) {
			targerFile.mkdirs();
		}
		String fileName = System.currentTimeMillis() + suffix;
		// 保存文件
		File uploadFile = new File(fileRealPath + fileName);
		FileCopyUtils.copy(file.getBytes(), uploadFile);

		ResponseUtil.print(response, ReturnCode.SUCCESS_CODE, fileName);
	}
}
